package cn.iocoder.an.module.mold.service.repairjob;

import cn.iocoder.an.framework.common.enums.UserTypeEnum;
import cn.iocoder.an.framework.security.core.LoginUser;
import cn.iocoder.an.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.an.framework.websocket.core.util.WebSocketFrameworkUtils;
import cn.iocoder.an.module.mold.dal.dataobject.repairaccount.RepairAccountDO;
import cn.iocoder.an.module.mold.dal.mysql.repairaccount.RepairAccountMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;

import cn.iocoder.an.module.mold.controller.admin.repairjob.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.repairjob.RepairJobDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.mold.dal.mysql.repairjob.RepairJobMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.module.mold.enums.ErrorCodeConstants.*;

/**
 * 模具维修任务 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RepairJobServiceImpl implements RepairJobService {

    @Resource
    private RepairJobMapper repairJobMapper;

    @Resource
    private RepairAccountMapper repairAccountMapper;

    @Override
    public Long createRepairJob(RepairJobSaveReqVO createReqVO) {
        // 插入
        RepairJobDO repairJob = BeanUtils.toBean(createReqVO, RepairJobDO.class);
        repairJobMapper.insert(repairJob);

        // 返回
        return repairJob.getId();
    }

    @Override
    public void updateRepairJob(RepairJobSaveReqVO updateReqVO) {
        // 校验存在
        validateRepairJobExists(updateReqVO.getId());
        // 更新
        RepairJobDO updateObj = BeanUtils.toBean(updateReqVO, RepairJobDO.class);
        repairJobMapper.updateById(updateObj);
    }

    @Override
    public void deleteRepairJob(Long id) {
        // 校验存在
        validateRepairJobExists(id);
        // 删除
        repairJobMapper.deleteById(id);
    }

    @Override
    public void deleteRepairJobListByIds(List<Long> ids) {
        // 删除
        repairJobMapper.deleteByIds(ids);
    }


    private void validateRepairJobExists(Long id) {
        if (repairJobMapper.selectById(id) == null) {
            throw exception(REPAIR_JOB_NOT_EXISTS);
        }
    }

    @Override
    public RepairJobDO getRepairJob(Long id) {
        return repairJobMapper.selectById(id);
    }

    @Override
    public PageResult<RepairJobDO> getRepairJobPage(RepairJobPageReqVO pageReqVO) {
        //获取当前用户
        if (!SecurityFrameworkUtils.getLoginUser().getUserType().equals(UserTypeEnum.ADMIN.getValue())) {//判断不是管理员
            LoginUser user = SecurityFrameworkUtils.getLoginUser();
            pageReqVO.setAssigneerId(user.getId());
        }
        return repairJobMapper.selectPage(pageReqVO);
    }


    @Override
    public Boolean modStatus(RepairJobModStatusVO repairJobModStatusVO) {
        Long id = repairJobModStatusVO.getId();
        Integer status = repairJobModStatusVO.getStatus();
        String data = repairJobModStatusVO.getData();
        LocalDateTime now = LocalDateTime.now();
        RepairJobDO jobDo = repairJobMapper.selectById(id);
        Integer jobStatus = jobDo.getStatus();

        RepairAccountDO accountDO = repairAccountMapper.selectOne(new LambdaQueryWrapper<RepairAccountDO>()
                .eq(RepairAccountDO::getJobId, jobDo.getId()));
        switch (status) {
            case 3:
                if (jobStatus != 2 && jobStatus != 4) {
                    throw exception(REPAIR_JOB_NOT_WORKREPORTED);
                }
                accountDO.setStatus(status);
                repairAccountMapper.updateById(accountDO);

                jobDo.setStatus(status);
                if (jobStatus == 2) {
                    jobDo.setJobReceiveTime(now);
                    jobDo.setJobStartTime(now);
                }
                repairJobMapper.updateById(jobDo);
                break;
            case 4:
                if (jobStatus != 3 && jobStatus != 5) {
                    throw exception(UPKEEP_JOB_NO_START);
                }
                jobDo.setStatus(status);
                accountDO.setStatus(status);
                repairAccountMapper.updateById(accountDO);
                jobDo.setJobStartTime(now);
                repairJobMapper.updateById(jobDo);
                break;
            case 5:
                if (status != 4) {
                    throw exception(UPKEEP_JOB_NO_START);
                }
                jobDo.setStatus(status);
                //更新待保养任务中的状态
                accountDO.setStatus(status);
                repairAccountMapper.updateById(accountDO);
                jobDo.setJobEndTime(now);
                repairJobMapper.updateById(jobDo);
                break;
            default:
                throw exception(UPKEEP_TASK_STATUS_ERROR);
        }

        return true;
    }


    @Override
    public Boolean workreport(RepairJobWorkReportVO workReportVO) {
        int upkeepStatus = 4;
        Long id = workReportVO.getId();
        RepairJobDO jobDo = repairJobMapper.selectById(id);
        jobDo.setRepairCategoryId(workReportVO.getRepairCategoryId());
        jobDo.setRepairerIds(workReportVO.getRepairerIds());
        jobDo.setJobStartTime(workReportVO.getJobStartTime());
        jobDo.setJobEndTime(workReportVO.getJobEndTime());
        jobDo.setJobExecutionDuration(workReportVO.getJobExecutionDuration());
        jobDo.setActualStartTime(workReportVO.getJobStartTime());
        jobDo.setActualEndTime(workReportVO.getJobEndTime());
        jobDo.setActualExecutionDuration(workReportVO.getJobExecutionDuration());
        jobDo.setWorkReportRemarks(workReportVO.getDescription());
        jobDo.setRepairCost(workReportVO.getRepairCost());
        jobDo.setStatus(upkeepStatus);
        repairJobMapper.updateById(jobDo);
        //修改状态
        RepairAccountDO accountDO = repairAccountMapper.selectOne(new LambdaQueryWrapper<RepairAccountDO>().eq(RepairAccountDO::getJobId, jobDo.getId()));
        accountDO.setStatus(upkeepStatus);
        repairAccountMapper.updateById(accountDO);
        return true;
    }

}